from tkinter import *  
from tkinter import messagebox  
from Enroll import EnrollWindow
from Main import Window
from middleWare import Middleware
from config.Instrument import Instrument_toml

class LoginWindow:  
    def __init__(self, master):  
        self.master = master  
        self.master.title("Login")  
        self.master.resizable(0, 0)  
        self.master.iconbitmap("static\\favicon.ico")  
        #设置背景颜色为 0xffffff
        self.master.configure(background="#ffffff")
        self.width = self.master.winfo_screenwidth()  
        self.height = self.master.winfo_screenheight()  
  
        self.x = (self.width - 600) // 2  
        self.y = (self.height - 400) // 2   
  
        self.master.geometry("{}x{}+{}+{}".format(600, 400, self.x, self.y))  


        #创建一个不可修改的文本域，大小为300*400
        self.text = Label(self.master, text=" ")
        self.text.configure(width=40,height=40,background="#3b80f9",fg="white")
        self.text.place(x=0, y=0)

        #创建一个文本域，文字为测试
        self.text2 = Label(self.master, text="欢迎使用！")
        #设置背景色为#3b80f9,字体大小为20，颜色为白色
        self.text2.configure(background="#3b80f9",font=("微软雅黑", 20),fg="white")
        self.text2.place(x=10, y=50)

        #创建一个文本域，文字为测试
        self.text2 = Label(self.master, text="——")
        #设置背景色为#3b80f9,字体大小为20，颜色为白色
        self.text2.configure(background="#3b80f9",font=("微软雅黑", 20),fg="white")
        self.text2.place(x=10, y=90)


        self.text2 = Label(self.master, text="欢迎使用学生考勤管理系统,本系统使用tkinter")
        #设置背景色为#3b80f9,字体大小为20，颜色为白色
        self.text2.configure(background="#3b80f9",fg="white")
        self.text2.place(x=10, y=170)

        self.text2 = Label(self.master, text="跳脱传统tkinter模式,使用类h5+css风格,打破")
        #设置背景色为#3b80f9,字体大小为20，颜色为白色
        self.text2.configure(background="#3b80f9",fg="white")
        self.text2.place(x=10, y=200)

        self.text2 = Label(self.master, text="传统tkinter设计模式,让您拥有眼前一亮的感觉,")
        #设置背景色为#3b80f9,字体大小为20，颜色为白色
        self.text2.configure(background="#3b80f9",fg="white")
        self.text2.place(x=10, y=230)

        self.text2 = Label(self.master, text="快来试试看吧！")
        #设置背景色为#3b80f9,字体大小为20，颜色为白色
        self.text2.configure(background="#3b80f9",fg="white")
        self.text2.place(x=10, y=260)

        self.text2 = Label(self.master, text="Designed by DataSphere Studio")
        #设置背景色为#3b80f9,字体大小为20，颜色为白色
        self.text2.configure(background="#3b80f9",fg="white")
        self.text2.place(x=10, y=360)

        self.text2 = Label(self.master, text="请输入信息")
        #设置背景色为#3b80f9,字体大小为20，颜色为白色
        self.text2.configure(background="#ffffff",fg="black",font=(15))
        self.text2.place(x=310, y=40)
        

        self.user = Label(self.master, text="账号")  
        #设置背景颜色为 0xffffff  
        self.user.configure(background="#ffffff")
        #位置在这里设置
        self.user.place(x=310, y=110)  
        self.password = Label(self.master, text="密码")
        #设置背景颜色为 0xffffff  
        self.password.configure(background="#ffffff")
        self.password.place(x=310, y=180)  
        self.txt_user = Entry(self.master, width=30)  
        #设置输入框颜色为 #f7f8fa
        self.txt_user.configure(background="#f7f8fa")
        self.txt_user.place(x=350, y=110)  
        self.txt_password = Entry(self.master, width=30)  
        #设置输入框颜色为 #f7f8fa
        self.txt_password.configure(background="#f7f8fa")
        self.txt_password.place(x=350, y=180)  
        self.txt_password.config(show="*")  
        self.txt_user.focus()  
        self.txt_user.bind("<Return>", lambda e: self.txt_password.focus())  
        self.btn = Button(self.master, text="登录",width=10, command=self.clicked)  
        self.btn.configure(background="#3b80f9",relief=FLAT,fg="white",padx=3,pady=3)
        self.btn.place(x=310, y=250)  


        #设置一个按钮，点击可以打开注册界面，宽度为100，高度为30
        self.btn_register = Button(self.master, text="注册", width=10, command=self.register)
        #设置按钮颜色为#3b80f9
        self.btn_register.configure(background="#3b80f9",relief=FLAT,fg="white",padx=3,pady=3)
        #设置按钮的位置
        self.btn_register.place(x=470, y=250)
    
    # 打开注册页面
    def register(self):
        Window = Toplevel(self.master)
        app = EnrollWindow(Window)
        app.start()
    def clicked(self):  
        user = self.txt_user.get()  
        password = self.txt_password.get()
        if user == "" or password == "":  
            messagebox.showinfo("提示", "账号或密码不能为空")  
            return
        try:
            mid = Middleware()
            result = mid.data_binding_user_by_id(user)
            user_t = result['nickname']
            password_t = result['password']
        except:
            messagebox.showinfo("提示", "账号或密码错误")
            return
        toml = Instrument_toml()
        
        if user == user_t and password == password_t:  
            toml.SetStrogeToml('nickname',user_t)
            self.master.destroy()
            app = Window()
            app.start()
    
        else:  
            messagebox.showinfo("提示", "账号或密码错误")  

        
        
    
    def start(self):  
        self.master.mainloop()
  
if __name__ == "__main__":  
    root = Tk()  
    app = LoginWindow(root)  
    root.mainloop()